package cn.com.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_match_prize;

import cn.com.api.service.ApiU_match_prizeService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_match_prize/")
public class ApiU_match_prizeController {
	@Autowired
	private ApiU_match_prizeService apiU_match_prizeService;

	private static Logger logger = Logger.getLogger(ApiU_match_prizeController.class);

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertU_match_prize")
	public void insertU_match_prize(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String prize_id = request.getParameter("prize_id");
		String match_id = request.getParameter("match_id");
		String prize_name = request.getParameter("prize_name");
		String prize_num = request.getParameter("prize_num");
		String prize_money = request.getParameter("prize_money");
		String winner_count = request.getParameter("winner_count");
		String prize_percent = request.getParameter("prize_percent");
		String remark = request.getParameter("remark");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (prize_id != null && !"".equals(prize_id.trim())) {
				ApiU_match_prize u_match_prize = new ApiU_match_prize();

				// u_match_prize.setPrize_id(prize_id);
				// u_match_prize.setMatch_id(match_id);
				// u_match_prize.setPrize_name(prize_name);
				// u_match_prize.setPrize_num(prize_num);
				// u_match_prize.setPrize_money(prize_money);
				// u_match_prize.setWinner_count(winner_count);
				// u_match_prize.setPrize_percent(prize_percent);
				// u_match_prize.setRemark(remark);
				// u_match_prize.setExt1(ext1);
				// u_match_prize.setExt2(ext2);
				// u_match_prize.setExt3(ext3);

				apiU_match_prizeService.insertU_match_prize(u_match_prize);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_match_prizeById")
	public void findU_match_prizeById(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiU_match_prize u_match_prize = apiU_match_prizeService.findU_match_prizeById(Integer.valueOf(id));

			// return
			map.put("datas", u_match_prize);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_match_prizes")
	public void findU_match_prizes(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<ApiU_match_prize> u_match_prizes = apiU_match_prizeService.findU_match_prizes();

			// return
			map.put("datas", u_match_prizes);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateU_match_prize")
	public void updateU_match_prize(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String prize_id = request.getParameter("prize_id");
		String match_id = request.getParameter("match_id");
		String prize_name = request.getParameter("prize_name");
		String prize_num = request.getParameter("prize_num");
		String prize_money = request.getParameter("prize_money");
		String winner_count = request.getParameter("winner_count");
		String prize_percent = request.getParameter("prize_percent");
		String remark = request.getParameter("remark");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");

		// String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (prize_id != null && !"".equals(prize_id.trim())) {
				ApiU_match_prize u_match_prize = new ApiU_match_prize();
				// 获取原数据
				u_match_prize = apiU_match_prizeService.findU_match_prizeById(Integer.valueOf(prize_id));
				// 修改
				// u_match_prize.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_match_prizeService.updateU_match_prize(u_match_prize);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteU_match_prize")
	public void deleteU_match_prize(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiU_match_prizeService.deleteU_match_prize(Integer.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
